package org.layuiframework.generator.engine.impl;

import org.layuiframework.generator.config.po.LayuiMapper;
import org.layuiframework.generator.config.po.LayuiTableField;
import org.layuiframework.generator.engine.TableFieldConverter;

import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class TableFieldConvertSelect implements TableFieldConverter {

    @Override
    public boolean canDeal(String comment) {
        final String N_PLUS_PATTREN = "^([^\\.|^;|^:]+\\.[^\\.|^;]+;?)+$";
        Pattern pattern = Pattern.compile(N_PLUS_PATTREN);
        Matcher matcher = pattern.matcher(comment);
        return matcher.find();
    }

    @Override
    public LayuiTableField deal(LayuiTableField layuiTableField, String comment) {
        layuiTableField.setSelectColumn(true);
        layuiTableField.setLayuiMapper(true);
        layuiTableField.setSource(Arrays.asList(comment.split(";")).stream().map(m -> {
            String [] values = m.split("\\.");
            return new LayuiMapper(values[0], values.length >= 2 ? values[1] : "");
        }).collect(Collectors.toList()));
        return layuiTableField;
    }
}
